#define _CRT_SECURE_NO_WARNINGS 1

#include<stdlib.h>

struct ListNode
{
    int val;
    ListNode* next;
};

typedef struct ListNode ListNode;

int ListSize(ListNode* phead)
{
    ListNode* pcur = phead;
    int size = 0;
    while (pcur)
    {
        size++;
        pcur = pcur->next;
    }
    return size;
}

void reverse(int* left, int* right)
{
    while (left < right)
    {
        int temp = *left;
        *left = *right;
        *right = temp;
        left++;
        right--;
    }
}

struct ListNode* reverseBetween(struct ListNode* head, int left, int right)
{
    int* arr = (int*)malloc(sizeof(int) * ListSize(head));
    ListNode* pcur = head;
    int index = 0;
    while (pcur)
    {
        arr[index++] = pcur->val;
        pcur = pcur->next;
    }
    reverse(arr + left - 1, arr + right - 1);
    pcur = head;
    for (int i = 0; i < ListSize(head); i++)
    {
        pcur->val = arr[i];
        pcur = pcur->next;
    }
    return head;
}